(defpackage :questions
  (:use :cl)
  (:export :questions))

(in-package :questions)

(defclass questions (exp:state)
  ((questions :initform nil :initarg :questions :accessor questions)
   (continue-button :accessor continue-button)))

(defun render-string (string font color)
  (let ((s (sdl:create-surface (sdl:get-font-size string :size :w :font font)
                               (sdl:get-font-size string :size :h :font font)
                               :pixel-alpha 0)))
    (sdl:clear-display gwee:*default-background-color* :surface s)
    (sdl:draw-string-blended-* string 0 0 :surface s :color color :font font)
    s))

(defclass legend (gwee:view)
  ((surface :initarg :surface :accessor surface)))

(defmethod gwee:draw-view ((view legend))
  (sdl:draw-surface-at-* (surface view)
                         (+ (gwee:view-cache-top-x view)
                            (- (gwee:view-width view)
                               (sdl:width (surface view))))
                         (gwee:view-cache-top-y view)))

(defun make-legend ()
  (let* ((key (gwee:color 255 200 200))
         (lines (loop for i in '("Strongly Disagree"
                                 "Disagree"
                                 "Agree"
                                 "Strongly Agree")
                   collect (sdl:rotate-surface 60 :surface (render-string i (gwee:default-font 12) gwee:*default-foreground-color*)
                                                   :smooth t)))
         (main (sdl:create-surface (loop for i in lines
                                      :sum (sdl:width i))
                                   (loop for i in lines
                                      :maximize (sdl:height i)))))
    (sdl:clear-display key :surface main)
    (loop for i in lines
       for x from 0 by 33
       do (setf (sdl:color-key-enabled-p i) t
                (sdl:color-key i) key)
       (sdl:draw-surface-at-* i x (- (sdl:height main) (sdl:height i)) :surface main))
    (setf (sdl:color-key-enabled-p main) t
          (sdl:color-key main) key)
    (make-instance 'legend :surface main
                   :width (+ 10 (sdl:width main))
                   :height (sdl:height main))))

(defun click-radio (state id option)
  (exp:lg 'choose :question id :option option)
  (when (loop for i in (questions state)
           for j from 0
           always (some 'gwee:selected (gwee:radio-buttons-with-id j)))
    (setf (gwee:disabled-p (continue-button state)) nil)))

(defun make-radios (state question id)
  (macrolet ((click-fn (option)
               `(lambda (b)
                  (declare (ignore b))
                  (click-radio state id ,option))))
    (make-instance 'gwee:row-layout
                   :padding 10
                   :height :minimum
                   :width :minimum
                   :subviews (list (make-instance 'gwee:radio-button :radio-id id :click-callback (click-fn :strongly-disagree))
                                   (make-instance 'gwee:radio-button :radio-id id :click-callback (click-fn :disagree))
                                   (make-instance 'gwee:radio-button :radio-id id :click-callback (click-fn :agree))
                                   (make-instance 'gwee:radio-button :radio-id id :click-callback (click-fn :strongly-agree))
                                   (make-instance 'gwee:text-area :data question
                                                  :font (gwee:default-font 24)
                                                  ;;:horizontal-justification :right
                                                  :vertical-justification :center
                                                  ;; :width 640
                                                  )))))

(defmethod exp:state-init (exp (state questions))
  (exp:lg 'start :questions (questions state))
  (make-instance 'gwee:column-layout
                 :x :center :y :center
                 :padding 80
                 :parent state
                 :subviews (list
                            (make-instance 'gwee:text-area :data "On the last problem..." :font (gwee:default-font 30))
                            (make-instance 'gwee:column-layout
                                           :height :minimum
                                           :width :minimum
                                           :padding 10
                                           :cell-gravity :top
                                           :subviews (cons (make-legend)
                                                           (loop for i in (questions state)
                                                              for j from 0
                                                              collect (make-radios state i j))))
                            (setf (continue-button state) (make-instance 'gwee:button :font (gwee:default-font 24) :data "Continue" :disabled-p t
                                                                  :click-callback (exp:dispatch-event-fn exp state exp:click-event :source :continue))))))

(exp:define-action continue ((state questions)
                             (click :source :continue))
  (exp:state-onward exp state))

(defmethod exp:state-clean-up (exp (state questions))
  (exp:lg 'end :answers (loop for i in (questions state)
                             for j from 0
                           for buttons = (gwee:radio-buttons-with-id j)
                           do (print `(buttons ,buttons))
                           collect (list j
                                         (loop for b in buttons
                                            for answer in '(:strongly-agree :agree :disagree :strongly-disagree)
                                            when (gwee:selected b)
                                            return answer)))))

(defmethod exp:state-dump append (exp (state questions))
  `(:questions ,(questions state)))
